package com.rest.demo.Controller;

import com.alibaba.fastjson.JSONObject;
import com.rest.demo.Service.Dysmtl;
import lombok.extern.slf4j.Slf4j;
import okhttp3.internal.http.HttpMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URISyntaxException;


@Slf4j
@RestController
@CrossOrigin(origins = "https://sfz.dysmt.cn")
public class UserController {

    String url1;


    @Value("${ServerSC.MainUrl}")
    String MainUrl;//请求code的主地址
    @Value("${ServerSC.MainUrlUser}")
    String MainUrlUser;//请求用户信息的api主地址
    @Value("${ServerSC.token}")
    String token;//请求token参数
    @Value("${ServerSC.authorize}")
    String authorize;//请求Code参数
    @Value("${ServerSC.base}")
    String base;//请求base参数
    @Value("${ServerSC.userinfo}")
    String userinfo;//请求base参数
    @Value("${ServerSC.client_id}")
    String client_id;//id值
    @Value("${ServerSC.client_secret}")
    String client_secret;//secret值
    @Value("${ServerSC.redirect_uri}")
    String redirect_uri;//定向网址值



    @Value("${APiUrl.API_ROOT}")
    String API_ROOT;//定向网址值

    @Autowired//没有这个服务不起作用
    private Dysmtl Dysmtl;//定义一个 DemoService 服务层


    @GetMapping("/GetUsersInfo")
    public String test(HttpServletResponse response, HttpServletRequest request) throws IOException, JSONException {
        String Code = request.getParameter("code");
         System.out.println(Code);
        if (Code == null) {
            response.sendRedirect(Dysmtl.ApiUrl("snsapi_userinfo",MainUrl,authorize,client_id,redirect_uri));
//            log.info(request.getRequestURI());
        } else {
//            log.info("Code" + Code);
            JSONObject UserBase = Dysmtl.GetUserInfomation(Dysmtl.GetToken(Code,MainUrl,authorize,client_id,redirect_uri,token,client_secret),MainUrlUser,userinfo);
//            log.info("UserBase" + UserBase.toString());

            return UserBase.toString();
        }
        return null;
    }

    @GetMapping("/GOCode")
    public void GOCode(HttpServletResponse response) throws IOException {
        response.sendRedirect(Dysmtl.ApiUrl("snsapi_userinfo",MainUrl,authorize,client_id,redirect_uri));
    }





    @ResponseBody

    @RequestMapping("/OutUsersInfo")

    public String OutInfo(String Code) throws IOException, JSONException {

//
        JSONObject UserBase = Dysmtl.GetUserInfomation(Dysmtl.GetToken(Code,MainUrl,authorize,client_id,redirect_uri,token,client_secret),MainUrlUser,userinfo);
            return UserBase.toString();
        }








    @GetMapping("/GetCode")
    public void test() {


    }


    @PostMapping(value = "/admin", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Object> postPCWithJson(@RequestBody Object body, HttpServletRequest request, HttpServletResponse response, HttpMethod httpMethod) throws URISyntaxException {
        // TODO
        return new ResponseEntity("", HttpStatus.OK);
    }


}
